package com.qcby.mycommunity_003.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qcby.mycommunity_003.entity.Menu;
import com.qcby.mycommunity_003.mapper.MenuMapper;
import com.qcby.mycommunity_003.service.MenuService;
import com.qcby.mycommunity_003.vo.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单管理 服务实现类
 * </p>
 *
 * @author admin
 * @since 2024-03-24
 */
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {
    @Autowired
    private MenuMapper menuMapper;
    @Autowired
    private MenuService menuService;
    @Override
    public List<MenuRecordVO> getMenusByUserId(Integer userId) {
        //1.根据用户id查询到该用户的角色，根据角色id查询到对应能访问的菜单
       List<Menu> menuList = this.menuMapper.getMenusByUserId(userId);
       //2.如果是父菜单就存在第一层
        List<MenuRecordVO> list = new ArrayList<>();
        for (Menu menu : menuList) {
            if (menu.getParentId() == 0) {
                MenuRecordVO menuRouterVO = new MenuRecordVO();
                //bean实体类的工具，将相同属性封装进去
                BeanUtils.copyProperties(menu, menuRouterVO);
                //没有的属性进行赋值
                MetaVo metaVO = new MetaVo();
                metaVO.setTitle(menu.getName());
                metaVO.setIcon(menu.getIcon());
                menuRouterVO.setMeta(metaVO);
                //3.生成children
                //父id＝0的菜单的id
                Integer menuId = menu.getMenuId();
                List<MenuRecordChildrenVo> children = new ArrayList<>();
                for (Menu child : menuList) {
                    if(child.getParentId() == menuId){
                        MenuRecordChildrenVo childVO = new MenuRecordChildrenVo();
                        BeanUtils.copyProperties(child, childVO);
                        MetaVo childMetaVO = new MetaVo();
                        childMetaVO.setTitle(child.getName());
                        childMetaVO.setIcon(child.getIcon());
                        childVO.setMeta(childMetaVO);
                        children.add(childVO);
                    }
                }
                menuRouterVO.setChildren(children);
                list.add(menuRouterVO);
            }
        }
        return list;
    }

    @Override
    public List<AllMenuParent> Menulist() {
        //1.寻找父目录
        QueryWrapper<Menu> queryWrapper1=new QueryWrapper();
        queryWrapper1.eq("parent_id",0);
        List<Menu> list1=menuService.list(queryWrapper1);
        List<AllMenuParent> list3=new ArrayList<>();
        for (Menu menu : list1) {
            AllMenuParent r=new AllMenuParent();
            BeanUtils.copyProperties(menu,r);
            r.setId(menu.getMenuId());
            //封装好了外面那层，现在要封装children
            QueryWrapper<Menu> queryWrapper2=new QueryWrapper();
            queryWrapper2.eq("parent_id",menu.getMenuId());
            List<Menu> list2 = menuService.list(queryWrapper2);
            List<AllMenuChildren> children=new ArrayList<>();
            for (Menu menu1 : list2) {
                AllMenuChildren c=new AllMenuChildren();
                BeanUtils.copyProperties(menu1,c);
                c.setId(menu1.getMenuId());
                children.add(c);
            }
            r.setChildren(children);
            list3.add(r);
        }
        return list3;
    }
}
